package com.test50_JVM;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * 引用队列，ReferenceQueue
 */
public class Demo07_ReferenceQueue {

    public static void main(String[] args) throws Exception {
        Object obj = new Object();  // 强引用
        ReferenceQueue<Object> queue = new ReferenceQueue<>();  // 引用队列
        WeakReference<Object> ref = new WeakReference<>(obj, queue);    // 弱引用
        System.out.println("【引用状态】是否被回收：" + ref.isEnqueued());
        System.out.println("【引用队列】" + queue.poll());
        obj = null;
        System.gc();
        Thread.sleep(1000);
        System.err.println("【引用状态】是否被回收：" + ref.isEnqueued());
        System.err.println("【引用队列】" + queue.poll());
    }
}
